namespace MessyMessengerShinyService.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Windows;

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Messaging;

    using WP7Contrib.Common;
    using WP7Contrib.Logging;
    using WP7Contrib.Services.Navigation;
    using WP7Contrib.Services.Storage;

    public abstract class ViewModelBaseWp7 : ViewModelBase
    {
        #region Constants and Fields

        private readonly INavigationService navigationService;
        private readonly IStorageService storageService;
        
        protected readonly ILog Log;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        protected ViewModelBaseWp7()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        /// <param name="messenger">
        /// The messenger.
        /// </param>
        protected ViewModelBaseWp7(IMessenger messenger)
            : base(messenger)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="log"></param>
        protected ViewModelBaseWp7(INavigationService navigationService, ILog log)
            : this(navigationService, new NullStorageService(), null, log)
        {
        }

        protected ViewModelBaseWp7(INavigationService navigationService, IStorageService storageService, ILog log)
            : this(navigationService, storageService, null, log)
        {
        }

        protected ViewModelBaseWp7(INavigationService navigationService, IMessenger messenger, ILog log)
            : this(navigationService, new NullStorageService(), messenger, log)
        {
        }

        protected ViewModelBaseWp7(INavigationService navigationService,  IStorageService storageService, IMessenger messenger, ILog log)
            : base(messenger)
        {
            this.navigationService = navigationService;
            this.storageService = storageService;
            this.Log = log;

            var assemblies = new List<Assembly>
                                 {
                                     this.GetType().Assembly,
                                     typeof(BaseModel).Assembly
                                 };
        }

        ~ViewModelBaseWp7()
        {
            this.Dispose();
        }

        #endregion

        public void Activate()
        {
            using (var storage = this.storageService.OpenTransient(this.GetType().Name))
            {
                this.IsBeingActivated(storage);
            }
        }

        public void Deactivate()
        {
            using (var storage = this.storageService.OpenTransient(this.GetType().Name))
            {
                this.IsBeingDeactivated(storage);
            }
        }

        #region Properties

        public INavigationService NavigationService
        {
            get
            {
                return this.navigationService;
            }
        }

        #endregion

        protected abstract void IsBeingActivated(IStorage storage);

        protected abstract void IsBeingDeactivated(IStorage storage);
  
        protected void SetPropertyAndNotify<T>(ref T currentValue, T newValue, string proertyName)
        {
            if (Equals(currentValue, newValue))
            {
                return;
            }

            currentValue = newValue;
            this.RaisePropertyChanged(proertyName);
        }

        protected void ExecuteOnUIThread(Action uiAction)
        {
            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                uiAction();
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(uiAction);
            }
        }
    }
}
